
"""
A wrapper for the bonobo component gtkhtml3
"""
import gtk
import gnome
import bonobo
import bonobo.ui

OAFIID = 'OAFIID:GNOME_GtkHTML_Editor:3.8'
EDITOR_IDL = 'IDL:GNOME/GtkHTML/Editor/Engine:1.0'
PERSIST_IDL = 'IDL:Bonobo/PersistStream:1.0'

class HTMLEditor(object):

    def __init__(self, parent_window=None, data=None):
        # Gnome Program
        self._prog = gnome.program_init('arg', 'arg')
        # Bonobo Window
        self._window = self._create_window()
        self._container_ref = self._create_container_ref()
        # Bonobo Widget
        self._widget = self._create_widget()
        self._widget_ref = self._create_widget_ref()
        # Properties
        self._widget_props = self._create_widget_props()
        self._set_html_mode()
        # Interfaces
        self._persist_iface = self._create_persist_iface()
        # UI
        self._vb = gtk.VBox(spacing=6)
        self._vb.set_border_width(6)
        self._hbox = gtk.HBox(spacing=6)
        self._vb.pack_start(self._hbox, expand=False)
        self._hbox.pack_start(gtk.Label('Title:'), expand=False)
        self._title = gtk.Entry()
        self._hbox.pack_start(self._title)
        self._vb.pack_start(self._widget)
        self._bb = gtk.HButtonBox()
        self._bb.set_spacing(12)
        self._bb.set_layout(gtk.BUTTONBOX_END)
        self._vb.pack_start(self._bb, expand=False)
        self._cancel_button = gtk.Button(stock=gtk.STOCK_CANCEL)
        self._cancel_button.connect('clicked', self._on_cancel)
        self._bb.pack_start(self._cancel_button)
        self._save_button = gtk.Button(stock=gtk.STOCK_SAVE)
        self._save_button.connect('clicked', self._on_save)
        self._bb.pack_start(self._save_button)
        self._window.set_contents(self._vb)
        self._window.connect('delete-event', self._on_delete_event)
        self._window.resize(720, 600)
        if parent_window:
            self._window.set_transient_for(parent_window)
        # Dialog-like behaviour
        self._retval = None
        if data is not None:
            self.run(data)

    def _on_cancel(self, button):
        self._cancel()

    def _on_save(self, button):
        self._save()

    def _on_delete_event(self, window, event):
        self._close()

    def _save(self):
        self._retval = self._title.get_text(), self.read()
        self._close()

    def _cancel(self):
        self._close()

    def _close(self):
        self._window.destroy()
        gtk.main_quit()

    def _set_html_mode(self):
        bonobo.pbclient_set_boolean(self._widget_props, 'FormatHTML', True)

    def _create_window(self):
        return bonobo.ui.Window('html-editor', 'HTML Editor')

    def _create_widget(self):
        return bonobo.ui.Widget(OAFIID, self._container_ref)

    def _create_widget_props(self):
        return self._widget_ref.getProperties()

    def _create_container_ref(self):
        return self._window.get_ui_container().corba_objref()

    def _create_widget_ref(self):
        return self._widget.get_objref()

    def _create_persist_iface(self):
        return self._widget_ref.queryInterface(PERSIST_IDL)

    def run(self, title='', data=''):
        self._window.show_all()
        self.load(data)
        self._title.set_text(title)
        gtk.main()
        return self._retval

    def load(self, data):
        data = data.replace('</BR>', '')
        data = data.replace('<BR />', '<BR>')
        data = data.replace('\n', '')
        stream_mem = bonobo.StreamMem(data, True, False)
        self._persist_iface.load(stream_mem.corba_objref(), 'text/html')

    def read(self):
        stream_mem = bonobo.StreamMem('', False, True)
        self._persist_iface.save(stream_mem.corba_objref(), 'text/html')
        buf = stream_mem.get_buffer()
        buf = buf.replace('\n', '')
        buf = buf.replace('<BR>', '<BR />')
        return buf



